const promiseify = require('../../utils/promiseify')
const wxLogin = promiseify(wx.login)
const wxRequest = promiseify(wx.request)
Page({

  data: {
    nickname:'默认昵称',
    avatar: "https://img-blog.csdnimg.cn/img_convert/b573b00bed7126db2c209ed01eb35189.png"
  },

   // 手机号实时验证
   getrealtimephonenumber(event){
    console.log(event)
  },

   // 手机号快速验证
   getPhoneNumber(event){
    // 通过事件对象event.detail中可以获取到code,code为动态令牌，
    // 将code发送给自己的后端服务api，调用微信接口（https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${appid}&secret=${appSecret}）换取access_token,
    // 在通过返回access_token调用微信相关接口（POST https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=ACCESS_TOKEN），
    // 将获取到的手机号返回给前端
    const code = event.detail.code
    wx.request({
      url: 'http://127.0.0.1:7000/wx/get_access_token',
      method:"POST",
      success(res){
        console.log('res=>',res.data.access_token)
        const access_token = res.data.access_token
        wx.request({
          url: 'http://127.0.0.1:7000/wx/getPhoneNumber',
          method:"POST",
          data:{
            code,
            access_token
          },
          success(res){
            console.log(res.data.phone_info.phoneNumber)
            wx.showToast({
              title: res.data.phone_info.phoneNumber,
              icon:'none'
            })
          }
        })
      },
    })
  },

  // 一键登录
  async login(){
    // 1. 获取动态令牌
    const {code} = await wxLogin()
    console.log('code=>',code)
    // 2. 传递code调用开发者服务器接口，获取openid
    const {data} = await wxRequest({
      url:'http://127.0.0.1:7000/wx/login',
      method:'POST',
      data:{code}
    })
    console.log(data)
    let {token} = data.data
    let {code:resCode,msg} = data
    if(resCode === '00000'){
      wx.showToast({
        title:msg,
      })
      wx.setStorageSync('token', token)
    }else {
      wx.showToast({
        title:'登录失败，请稍后再试',
      })
    }

   
   
  },

  getOpenid(){
    wx.login({
      success: (res) => {
        // 1. 动态令牌
        console.log('code=>',res.code)
        const code = res.code
        // 2. 将code传递给自己开发服务器
        wx.request({
          url: 'http://127.0.0.1:7000/wx/getOpenid',
          method:"POST",
          data:{code},
          success(result){
            wx.showToast({
              title: result.data.openid,
              icon:'none',
            })
          }
        })
        
      },
    })
  },

  nicknameChange(event) {
    console.log('nicknameChange', event.detail.value)
    this.setData({
      nickname:event.detail.value
    })
  },

  onChooseAvatar(event) {
    console.log(event)
    this.setData({
      // 也可以将路径上传给服务器
      avatar: event.detail.avatarUrl
    })

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage(obj) {
    console.log('分享的来源:', obj.from); // 分享的来源
    return {
      title: '分享的标题',
      imageUrl: "/assets/images/sougou.jpg",
      path: "/pages/order/order"
    }
  },

  // 分享到朋友圈
  onShareTimeline() {
    return {
      title: '帮我看一刀',
      query: "userid=5",
      imageUrl: "/assets/images/sougou.jpg"
    }
  },
})